﻿'
' PDSplitter - http://www.dotnetcode.it
' Copyright (c) 2011
' by DotNetCode.IT
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
'


Imports System.Collections.Generic
Imports System.Text
Imports System.Deployment.Application
Imports Microsoft.Win32
Imports System.Windows.Forms
Imports System.Reflection
Imports System.Text.RegularExpressions


Public Class ClickOnce
    ''' <summary>
    ''' Gets the published version.
    ''' </summary>
    ''' <value>The published version.</value>
    Public Shared ReadOnly Property PublishedVersion() As Version
        Get
            Dim version As Version = Assembly.GetEntryAssembly().GetName().Version
            If ApplicationDeployment.IsNetworkDeployed Then
                Dim ad As ApplicationDeployment = ApplicationDeployment.CurrentDeployment
                version = ad.CurrentVersion
            End If
            Return version
        End Get
    End Property

    ''' <summary>
    ''' Registers the type of the file.
    ''' </summary>
    ''' <param name="FileType">Type of the file.</param>
    Public Shared Sub RegisterFileType(ByVal FileType As [String])
        If ApplicationDeployment.IsNetworkDeployed AndAlso ApplicationDeployment.CurrentDeployment.IsFirstRun Then
            Dim pathToApp As RegistryKey = Registry.ClassesRoot.CreateSubKey(ApplicationKey)
            pathToApp.CreateSubKey("DefaultIcon").SetValue([String].Empty, Application.ExecutablePath)
            pathToApp.CreateSubKey("shell").CreateSubKey("open").CreateSubKey("command").SetValue([String].Empty, "explorer """ & PathToApplication & "?LoadFile=%1""")

            Registry.ClassesRoot.CreateSubKey(FileType).SetValue([String].Empty, ApplicationKey)
        End If

    End Sub

    ''' <summary>
    ''' Gets the path to application.
    ''' </summary>
    ''' <value>The path to application.</value>
    Public Shared ReadOnly Property PathToApplication() As [String]
        Get
            Dim startUpURL As String = [String].Empty
            If ApplicationDeployment.IsNetworkDeployed AndAlso ApplicationDeployment.CurrentDeployment.UpdateLocation IsNot Nothing Then
                startUpURL = ApplicationDeployment.CurrentDeployment.UpdateLocation.AbsoluteUri

                If ApplicationDeployment.CurrentDeployment.UpdateLocation.Query IsNot Nothing AndAlso ApplicationDeployment.CurrentDeployment.UpdateLocation.Query.Length > 0 Then
                    startUpURL = startUpURL.Replace(ApplicationDeployment.CurrentDeployment.UpdateLocation.Query, [String].Empty)
                End If
            End If

            Return Uri.UnescapeDataString(startUpURL)
        End Get
    End Property
    ''' <summary>
    ''' Gets the application key.
    ''' </summary>
    ''' <value>The application key.</value>
    Public Shared ReadOnly Property ApplicationKey() As [String]
        Get
            Return Regex.Replace(Application.ProductName, "[^A-z0-9]", "")
        End Get
    End Property
    ''' <summary>
    ''' Makes the application autostart.
    ''' </summary>
    Public Shared Sub MakeApplicationAutostart()
        Dim StartUp As RegistryKey = Registry.CurrentUser.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\RunOnce")
        ' This Ensures that the only Latest version will autostart
        If ApplicationDeployment.IsNetworkDeployed AndAlso ApplicationDeployment.CurrentDeployment.UpdateLocation IsNot Nothing Then
            StartUp.SetValue(ApplicationKey, "explorer """ & PathToApplication & "?autostart""")
        End If
    End Sub

    Public Shared Sub CheckForShortcut()
        If ApplicationDeployment.IsNetworkDeployed Then

        Dim ad As ApplicationDeployment = ApplicationDeployment.CurrentDeployment
        If ad.IsFirstRun Then
            Dim code As Assembly = Assembly.GetExecutingAssembly()
            Dim company As String = String.Empty
            Dim description As String = String.Empty
            If Attribute.IsDefined(code, GetType(AssemblyCompanyAttribute)) Then
                Dim ascompany As AssemblyCompanyAttribute = DirectCast(Attribute.GetCustomAttribute(code, GetType(AssemblyCompanyAttribute)), AssemblyCompanyAttribute)
                company = ascompany.Company
            End If
            If Attribute.IsDefined(code, GetType(AssemblyDescriptionAttribute)) Then
                Dim asdescription As AssemblyDescriptionAttribute = DirectCast(Attribute.GetCustomAttribute(code, GetType(AssemblyDescriptionAttribute)), AssemblyDescriptionAttribute)
                description = asdescription.Description
            End If

            If company <> String.Empty AndAlso description <> String.Empty Then
                Dim desktopPath As String = String.Empty
                desktopPath = String.Concat(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "\", description, ".appref-ms")
                Dim shortcutName As String = String.Empty
                shortcutName = String.Concat(Environment.GetFolderPath(Environment.SpecialFolder.Programs), "\", company, "\", description, ".appref-ms")
                System.IO.File.Copy(shortcutName, desktopPath, True)
            End If
        End If
        End If

    End Sub

End Class
